package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;


@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private AddressService addressService;

//    @PreAuthorize("hasAnyAuthority('ADDRESS_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            System.out.println(address);
            addressService.add(address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Address address = addressService.findById(id);
            return new Result(true,MessageConstant.GET_ADDRESS_SUCCESS,address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ADDRESS_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address){
        try {
            Address byId = addressService.findById(address.getId());
            //删除redis缓存
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,byId.getImg());
            addressService.edit(address);
            return new Result(true,MessageConstant.EDIE_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Address a = addressService.findById(id);
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,a.getImg());
            addressService.delete(id);
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (RuntimeException e) {
            //catch自定义错误并返回
            return new Result(false, e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }
}
